package cn.biplam.front.component.view.systemManage.modelset;

import cn.biplam.common.bean.md.MdTheme;
import cn.biplam.common.bean.md.ModelThemeRootObj;
import cn.biplam.common.client.DataFilter;
import cn.biplam.common.client.Operator;
import cn.biplam.common.client.OperatorResult;
import cn.biplam.common.opcode.mdManage.TypeCode.OptModelIndexCode;
import cn.biplam.common.paramters.SysName;
import cn.biplam.common.paramters.ModelType.Model_Object_Type;
import cn.biplam.common.struc.BaseStruc;
import cn.biplam.common.tools.DEBUG;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.KvMap;
import cn.biplam.common.wrapper.UserInfo;
import cn.biplam.front.component.AjxResponse;
import cn.biplam.front.component.data.common.ButtonComp;
import cn.biplam.front.component.data.common.CaptionComp;
import cn.biplam.front.component.view.systemManage.modelset.manage.ModelThemeAdd;
import cn.biplam.front.component.view.systemManage.modelset.manage.ModelThemeModify;
import cn.biplam.front.html.HtmlDiv;
import cn.biplam.front.html.HtmlText;
import cn.biplam.front.html.HtmlValue;
import cn.biplam.front.html.base.HtmlWindow;
import cn.biplam.front.html.base.Const.Align_Type;
import cn.biplam.front.html.base.Const.Float_Type;
import cn.biplam.front.html.base.Const.Window_Event;
import cn.biplam.front.htmlscript.script.AjxScript;
import cn.biplam.front.htmlscript.script.PopupScript;
import cn.biplam.front.template.base.ButtonGroup;
import cn.biplam.front.template.buttons.NewButton;
import cn.biplam.front.template.buttons.RefreshButton;
import cn.biplam.front.template.control.FlowTable;
import cn.biplam.front.template.control.table.TablePropertys;
import cn.biplam.front.template.control.table.TablePropertys.Col_Content_Type;
import cn.biplam.front.template.pageFrame.mdManage.MdObject;
import cn.biplam.front.template.pageFrame.mdManage.MdPage;
import cn.biplam.front.template.pageFrame.mdManage.MdTable;

/**
 * 模型设置管理页面(多种业务模型通用)
 * @author WangSong 2009-7-22下午02:44:31
 * @version 1.0
 */
public class ModelSetManage extends AjxResponse {

	public static final String cId = "8205";
	public static final String aId_main = cId+"01";
	public static final String aId_delete = cId+"02";
	public static final String aId_delete_opt = cId+"03";
	
	private boolean showButton=true;

	@Override
	protected HtmlWindow ajxGet() throws ErrorMessage {
		if(aId_main.equals(actionId)){
			return getMainPage(mUser, sHomeUrl);
		}if(aId_delete.equals(actionId)){
			return getDelete(recordId,mUser);
		}if(aId_delete_opt.equals(actionId)){
			return getDeleteOpt(recordId,mUser);
		}else{
			throw new ErrorMessage("actionid is not found:"+actionId+"!");
		}
	}

	/**
	 * 取模型设置主页面
	 * @param mUser  用户信息 
	 * @param sHomeUrl 系统路径
	 * @return HtmlWindow
	 * @throws ErrorMessage
	 */
	private HtmlWindow getMainPage(UserInfo mUser,String sHomeUrl) throws ErrorMessage{
		MdPage page = new MdPage(cId,sHomeUrl,mUser); 
		/** 设置界面标题 */
		page.setTitleDesc(CaptionComp.get(mUser, "302"));
		/** 设置界面标题logo */
		page.setTitleLogo("/images/sm_pm.gif");
		/** 设置管理区*/
		page.putManage(getManagePage(mUser, sHomeUrl));
		HtmlWindow mainPage = page.get();
		return mainPage;
	}

	/**
	 * 取数据源管理的主页面
	 * @param mUser 用户信息
	 * @return HtmlWindow
	 * @throws ErrorMessage 
	 */
	private HtmlWindow getManagePage(UserInfo mUser,String sHomeUrl) throws ErrorMessage{
		String domainID = cId+"1";
		String tablePageID=domainID+"t";
		MdObject mdo = new MdObject(domainID);
		MdTable table=new MdTable(tablePageID,recordId);
		table.putObjectCol(getTable(mUser,tablePageID+"2"));
		mdo.putTable(table.get());
		if(showButton){
			/** 放入操作按钮组件 */
			mdo.putOperator(getButtonGroup(recordId, mUser));
		}
		return mdo.get();
	}

	/**
	 * 主页面数据源信息表格
	 * @param mUser 用户信息
	 * @return HtmlWindow
	 * @throws ErrorMessage 
	 */
	private HtmlWindow getTable(UserInfo mUser,String tablePageID) throws ErrorMessage{
		ModelThemeRootObj mtro = null;
		try{
			mtro = ModelThemeRootObj.getNewInstance(mUser,recordId);
		}catch(ErrorMessage e){
			StringBuilder sb = new StringBuilder();
			e.getDescription(sb);
			if(sb!=null && sb.indexOf("java.lang.ClassNotFoundException")!=-1){
				DEBUG.println("【Error】ModelType "+recordId+" maybe not publish");
				this.showButton=false;
				e.printStackTrace();
			}else{
				throw e;
			}
		}

		OperatorResult sr = null;
		if(mtro!=null){
			sr = mtro.getSelectResult();
		}
		/** 表主键 */
		String mainKey="1";
		if(TOOLS.exist(sr)){
			mainKey=mtro.getTHEME_ID();
			/** 创建表对象 */
			FlowTable st=new FlowTable(tablePageID,mainKey,getOperator(mtro)); 
			/** 将数据集放入表格 */
			st.put(sr);
			st.setHeight("480");
			st.putExtend(new HtmlValue(null,"h,modelType",recordId));
			st.putExtend(new HtmlValue(null,"h,"+mtro.getTHEME_TYPE(),
					Model_Object_Type.ROOT.toString()));
			HtmlWindow win = st.get();
			return win;
		}else{
			return  new HtmlDiv();
		}
	}

	/**
	 * 设置操作按钮
	 * @param property
	 * @throws ErrorMessage
	 */
	private TablePropertys getOperator(ModelThemeRootObj mtro) throws ErrorMessage{
		TablePropertys property = new TablePropertys(sHomeUrl, mUser);
		property.putCol(mtro.getTHEME_NAME(), CaptionComp.get(mUser,"300"));
		property.putCol(mtro.getTHEME_DESC(), CaptionComp.get(mUser,"301"));
		property.putCol(mtro.getDSNAME(), CaptionComp.get(mUser,"52"));
		property.putCol("operator",
				Col_Content_Type.BUTTON, 
				CaptionComp.get(mUser,"20"), 
				ButtonComp.get(mUser, "2"),
				ButtonComp.get(mUser, "27"));
		property.setDisplayRowIndex(true);
		/** 设置动作*/
		property.setColActionPr("operator", aId_delete,cId);
		property.setColActionPr("operator", ModelThemeModify.Modify_Theme,cId);
		return property;
	}

	/**
	 * 生成操作按钮并绑定按钮事件
	 * @param mUser
	 * @return
	 * @throws ErrorMessage
	 */
	private HtmlWindow getButtonGroup(String recordId,UserInfo mUser) throws ErrorMessage{
		ButtonGroup operatorButtons=new ButtonGroup("",Float_Type.Left);
		if(recordId!=null){
			operatorButtons.put(new RefreshButton(mUser,cId,aId_main,recordId).get());
			operatorButtons.put(new NewButton(mUser,cId,ModelThemeAdd.Add_Theme,recordId).get());	
		}
		return operatorButtons.get();
	}

	/**
	 * 删除数据源
	 * @param recordId  数据源id
	 * @param mUser     用户信息
	 * @return  HtmlWindow
	 * @throws ErrorMessage 
	 */
	private HtmlWindow getDelete(String recordId,UserInfo mUser) throws ErrorMessage{
		HtmlWindow delete = new HtmlDiv();
		String themeId = recordId;
		String modelType = params.getByKey("modelType");
		/** 生成删除确认页面*/
		MdTheme mto = MdTheme.getNewInstance(mUser,themeId,modelType);
		delete.put(new HtmlText(null,null,"确认删除'"+mto.getName()+"'?"));
		/** 设置居中样式*/
		delete.setAlign(Align_Type.center);
		delete.setStyleWidth("100");
		delete.setStyleHeight("50");
		/** 绑定确认删除脚本*/
		//设置提交动作附加参数
		KvMap<String, String> submitParams = new KvMap<String, String>();
		submitParams.put(".rid", themeId);
		submitParams.put("modelType", modelType);
		//生成提交脚本
		String script = AjxScript.getSpParamsNoRefresh(cId, //提交容器ID
				aId_delete_opt,                       //提交动作ID
				submitParams                          //提交附加参数
				);
		
		delete.bind(SysName.PromptSubmitID, Window_Event.OnClick, PopupScript.prSubmit(script));
		return delete;
	}

	/**
	 * 删除数据源
	 * @param recordId
	 * @param mUser
	 * @return
	 * @throws ErrorMessage 
	 */
	private HtmlWindow getDeleteOpt(String recordId,UserInfo mUser) throws ErrorMessage{
		HtmlWindow delete = new HtmlDiv();
		if(!this.themeIsUsed(recordId, mUser,params.getByKey("modelType"))){
			ModelThemeRootObj mtro = ModelThemeRootObj.getNewInstance(mUser,params.getByKey("modelType"));
			DataFilter filter = new DataFilter();
			filter.put(mtro.getTHEME_ID(), recordId);

			mtro.delete(filter);
		}else{
			delete.bind("alert('主题下有内容,不能删除');");
		}
		/** 绑定刷新父页面脚本*/
		delete.bind(AjxScript.getA(ModelSetManage.cId,ModelSetManage.aId_main,params.getByKey("modelType")));
		return delete;
	}

	/**
	 * 主题是否被引用
	 * @param recordId  主题ID
	 * @param mUser     用户信息
	 * @return
	 * @throws ErrorMessage 
	 */
	private boolean themeIsUsed(String themeId,UserInfo mUser,String modelType) throws ErrorMessage{
		DataFilter filter = new DataFilter();
		filter.put(BaseStruc.IndexTableStruc.NODE_TYPE,modelType);
		filter.put(BaseStruc.IndexTableStruc.NODE_CODE,themeId);
		filter.put(BaseStruc.IndexTableStruc.OPERATOR_TYPE,Model_Object_Type.ROOT.toString());
		OperatorResult sr = Operator.get(OptModelIndexCode.Query_Have_Child, mUser.getUserID(), filter);
		if(TOOLS.exist(sr)){
			return true;
		}
		return false;
	}
}
